import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class AddPanel extends JPanel {

	private JButton enterButton;
	private JLabel buyInLabel;
	private JLabel placeLabel;
	private JLabel earningLabel;
	private JTextField buyInTextField;
	private JTextField placeTextField;
	private JTextField earningTextField;

	public AddPanel() {
		this.setLayout(new BorderLayout());
		createDataIntroductionPanel();
		enterButton = new JButton("Enter session Data");
		class ClickButtonListener implements ActionListener {
			public void actionPerformed(ActionEvent event) {
				GameStat gs = new GameStat();
				gs.setBuyIn(Double.parseDouble(buyInTextField.getText()));
				gs.setPlace(Integer.parseInt(placeTextField.getText()));
				gs.setEarning(Double.parseDouble(earningTextField.getText()));
				buyInTextField.setText("");
				placeTextField.setText("");
				earningTextField.setText("");
				File file = new File("stat.txt");
				try {
					WriteText.writeGameStatLine(gs, file);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		ActionListener listener = new ClickButtonListener();
		enterButton.addActionListener(listener);
		this.add(enterButton, BorderLayout.SOUTH);
	}

	private void createDataIntroductionPanel() {
		JPanel introductionPanel = new JPanel();
		introductionPanel.setLayout(new GridLayout(3, 2));
		buyInLabel = new JLabel("Buy-in: ");
		placeLabel = new JLabel("Place: ");
		earningLabel = new JLabel("Earnings: ");
		buyInTextField = new JTextField();
		buyInTextField.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e){
				if(e.getKeyCode() == KeyEvent.VK_ENTER){
					placeTextField.grabFocus();
				}
			}
		});
		placeTextField = new JTextField();
		placeTextField.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e){
				if(e.getKeyCode() == KeyEvent.VK_ENTER){
					earningTextField.grabFocus();
				}
			}
		});
		earningTextField = new JTextField();
		earningTextField.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e){
				if(e.getKeyCode() == KeyEvent.VK_ENTER){
					enterButton.doClick();
				}
			}
		});
		introductionPanel.add(buyInLabel);
		introductionPanel.add(buyInTextField);
		introductionPanel.add(placeLabel);
		introductionPanel.add(placeTextField);
		introductionPanel.add(earningLabel);
		introductionPanel.add(earningTextField);
		this.add(introductionPanel, BorderLayout.NORTH);
	}
}
